// pages/modify/modify.js
const api = require("../../config/api.js")
var app = getApp(); // 取得全局App
Page({

  /**
   * 页面的初始数据
   */

  data: {

    items: [{
      name: 1,
      value: '学生',
      checked: 'true'
    },
    {
      name: 2,
      value: '老师'
    },
  ],
    user: {
      username: '',
      password: '',
      classRoom: '',
      status: '1',
      tel: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.userId == null) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return;
    }
    var that=this
    wx.request({
      url: api.findOne + "?userId=" + app.globalData.user.userId,
      method: "GET",
      success(res) {
        console.log(res)
        that.setData({
          user:res.data.data
        })
      }
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getUserame(even) {
    console.log(even.detail.value)
    this.setData({
      user: {
        username: even.detail.value,
        password: this.data.user.password,
        classRoom: this.data.user.classRoom,
        status: this.data.user.status,
        tel: this.data.user.tel
      }
    })
  },
  getPassword(even) {
    console.log(even.detail.value)
    this.setData({
      user: {
        username: this.data.user.username,
        password: even.detail.value,
        classRoom: this.data.user.classRoom,
        status: this.data.user.status,
        tel: this.data.user.tel
      }
    })
  },
  getclassRoom(even) {
    console.log(even.detail.value)
    this.setData({
      user: {
        username: this.data.user.username,
        password: this.data.user.password,
        classRoom: even.detail.value,
        status: this.data.user.status,
        tel: this.data.user.tel
      }
    })
  },
  radioChange: function (e) {
    this.setData({
      user: {
        username: this.data.user.username,
        password: this.data.user.password,
        status: e.detail.value
      },
      classRoom: this.data.user.classRoom,
      tel: this.data.user.tel
    })

  },
  getTel: function (even) {
    this.setData({
      user: {
        username: this.data.user.username,
        password: this.data.user.password,
        classRoom: this.data.user.classRoom,
        status: this.data.user.status,
        tel: even.detail.value,
      }
    })
  },
  register() {
    if (app.globalData.userId == null) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return;
    }
    this.data.user.userId=app.globalData.user.userId
    this.data.user.totalTime=app.globalData.user.totalTime
    this.data.user.role=app.globalData.user.role
    this.data.user.avatarUrl=app.globalData.user.avatarUrl
    console.log(this.data.user)
    var that = this
    if (this.data.user.username.length == 0 || this.data.user.password.length == 0  || this.data.user.tel == 0) {
      wx.showToast({
        title: '所有都要认真填哦',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    wx.showModal({
        title: '提示',
        content: '您确定修改吗',
        success(res) {
          if (res.confirm) {    
          wx.request({
            url: api.update,
            method: "POST",
            data: that.data.user,
            success: function (result) {
              if (result.statusCode == 200) {
                console.log(result)
                app.globalData.user = result.data.data
                wx.switchTab({
                  url: '../mine/mine',
                  success: function () {
                    wx.showToast({
                      title: '修改成功',
                      duration: 2000
                    })
                  }
                })
              } else if (result.statusCode == 202) {
                wx.showToast({
                  title: '修改内容和之前相同',
                  icon: 'none'
                })
              }
            }
          })
        }
      }
      })

  }
})
